import Boom from "@hapi/boom";
import Joi from "joi";

export const validatePayload = (schema: Joi.Schema) => {
  return {
    payload: (request: any, h: any, err: any) => {
      if (err) {
        throw Boom.badRequest("Invalid request payload");
      }
      const { error } = schema.validate(request);
      console.log(error);

      if (error) {
        throw Boom.badRequest(error);
      }
      return request;
    },
  };
};

export const validateQuery = (schema: Joi.Schema) => {
  return {
    query: (query: any, h: any, err: any) => {
      const { error } = schema.validate(query);
      if (error) {
        throw Boom.badRequest(error.message);
      }
      return query;
    },
  };
};
